#include "wnd_dialog.h"
#include "wnd_widget.h"
#include "wnd_handle.h"








static int WND_DLG_Proc(HWND hDlg, int message, WPARAM wParam, LPARAM lParam)
{
	int s32Num;
	WND_WIDGET_S *pstWnd = (WND_WIDGET_S*)GetWindowAdditionalData(hDlg);
	HDC u32Hdc;
	WIDGET_COMMON_S *pstCommon = &pstWnd->stCommon;
	WND_ATTR_S *pstAttr = &pstCommon->stAttr;
	//unsigned int u32WndX = (pstAttr->u32WndX * stGlbMainWndHandle.s32ScreenWidth) / UI_COORD_MAX;
	//unsigned int u32WndY = (pstAttr->u32WndY * stGlbMainWndHandle.s32ScreenHeight) / UI_COORD_MAX;
	unsigned int u32WndWidth = (pstAttr->u32WndWidth * stGlbMainWndHandle.s32ScreenWidth) / UI_COORD_MAX;
	unsigned int u32WndHeight = (pstAttr->u32WndHeight * stGlbMainWndHandle.s32ScreenHeight) / UI_COORD_MAX;

	switch (message){
	case MSG_INITDIALOG:
	{
		//SetWindowBkColor(hDlg, COLOR_red);


		//SLOG(TDBG, "对话框 [%s] 初始化\n", pstWnd->stCommon.stAttr.s8Name);
		if(pstWnd) {

			pstWnd->stCommon.stAttr.u32WndId = hDlg;
			WND_WIDGET_EnbleStly(pstWnd);



			for(s32Num = 0; s32Num < pstWnd->s32WidgetNum; s32Num++) {
				//WND_WIDGET_Create(&pstWnd->pstWidget[s32Num], pstWnd->stCommon.stAttr.u32WndId);
				WND_WIDGET_CreateSwitch(&pstWnd->pstWidget[s32Num], hDlg,
									(pstWnd->pstWidget[s32Num].stCommon.stAttr.u32WndX*u32WndWidth) / UI_COORD_MAX,
									(pstWnd->pstWidget[s32Num].stCommon.stAttr.u32WndY*u32WndHeight) / UI_COORD_MAX,
									(pstWnd->pstWidget[s32Num].stCommon.stAttr.u32WndWidth*u32WndWidth) / UI_COORD_MAX,
									(pstWnd->pstWidget[s32Num].stCommon.stAttr.u32WndHeight*u32WndHeight) / UI_COORD_MAX);

			}
		}

		break;
	}
	break;
	case MSG_PAINT:
	{

		u32Hdc = GetDC(pstWnd->stCommon.stAttr.u32WndId);

		for(s32Num = 0; s32Num < pstWnd->s32WidgetNum; s32Num++) {

			if(pstWnd->pstWidget[s32Num].enType == WND_WIDGET_TYPE_STATIC_PICTURE_E) {
				//FillBoxWithBitmap (u32Hdc, 0, 0, 100,
				//	100, &pstWnd->pstWidget[s32Num].unPrivate.stStaticPicture.stBitMap);
			}

		}
		ReleaseDC(u32Hdc);

	}
	break;
	case MSG_CLOSE:
		QLOG(TDBG, "对话框结束\n");
		WND_WIDGET_Destroy(pstWnd);
		EndDialog(hDlg, 0);
		return 0;
	}
	return DefaultDialogProc(hDlg, message, wParam, lParam);
}









int WND_DLG_OkButtonProc(HWND u32Wnd, int s32Message, WPARAM u32Param, LPARAM u64Param)
{
	SLOG(TDBG, "结束对话框\n");

	SendMessage(GetParent(u32Wnd), MSG_CLOSE, 0, 0);
	return 0;
}


static int WND_DLG_PicPproc (HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
	WND_WIDGET_S *pstWnd = (WND_WIDGET_S*)GetWindowAdditionalData(hWnd);
	switch (message) {
		case MSG_PAINT:
		{
			HDC hdc = BeginPaint (hWnd);
			FillBoxWithBitmap (hdc, 0, 0, 100,
				100, &pstWnd->unPrivate.stStaticPicture.stBitMap);
			EndPaint (hWnd, hdc);
			return 0;
		}
	}
	return DefaultContainerProc (hWnd, message, wParam, lParam);
}


WND_WIDGET_S stGlbWndDlgOk[] = {

		{
		.enType = WND_WIDGET_TYPE_STATIC_PICTURE_E,
		.unPrivate = {
			.stStaticPicture= {
				.s8MapPath = CTRL_PATH"/""dialog/finsh.png",
			},
		},
		.stCommon = {
			.stAttr = {
				.s8Name = "图片",
				.u32WndId = 0,
				.u32Group = 0,
				//.u32ParentWndId = HWND_DESKTOP,
				.u64Style = SS_BITMAP | WS_EX_TRANSPARENT,
				//.u64ExStyle = WS_EX_NONE,
				.u32Id = 5568,
				.u32Id = 1110,
				.u32WndX = (UI_COORD_MAX * 0)/2,
				.u32WndY = (UI_COORD_MAX * 1)/4,
				.u32WndWidth = (UI_COORD_MAX * 2)/6,
				.u32WndHeight = (UI_COORD_MAX * 2)/6,
				.enBkColor = UI_COLOR_DEFAULT_E,
				.bShowNow = true,
				//.bBorder = true,
			},
			.stFops = {
				.CallBack = WND_DLG_PicPproc,
			},
			//WND_STA_S stSta;
		},

		//.pstWidget = stGlbMainWndCtrl,
		//.s32WidgetNum = sizeof(stGlbMainWndCtrl) / sizeof(stGlbMainWndCtrl[0]),
	},

	{
		.enType = WND_WIDGET_TYPE_STATIC_TEXT_E,
		.stCommon = {
			.stAttr = {
				.s8Name = "保存参数成功",
				.u32WndId = 0,
				.u32Group = 2,
				.u32Id = 1112,
				.u32WndX = (UI_COORD_MAX*3)/6,
				.u32WndY = (UI_COORD_MAX*2)/6,
				.u32WndWidth = (UI_COORD_MAX*2)/6,
				.u32WndHeight = (UI_COORD_MAX*1)/6,
				.enBkColor = UI_COLOR_DEFAULT_E,
				.bShowNow = true,
				//Border = true,
			},
			.stFops = {
				//allBack = WIDGET_TEST_OnDlgTestButtonProc,
			},
		},

		//.pstWidget = stGlbMainWndCtrl,
		//.s32WidgetNum = sizeof(stGlbMainWndCtrl) / sizeof(stGlbMainWndCtrl[0]),
	},
	{
		.enType = WND_WIDGET_TYPE_BUTTON_E,
		.stCommon = {
			.stAttr = {
				.s8Name = "确认",
				.u32WndId = 0,
				.u32Group = 2,
				.u32Id = 116,
				.u32WndX = (UI_COORD_MAX*4)/6 + (UI_COORD_MAX*1)/12,
				.u32WndY = (UI_COORD_MAX*4)/6,
				.u32WndWidth = (UI_COORD_MAX*1)/6,
				.u32WndHeight = (UI_COORD_MAX*1)/6,
				.enBkColor = UI_COLOR_DEFAULT_E,
				.bShowNow = true,
				.bBorder = true,
			},
			.stFops = {
				.CallBack = WND_DLG_OkButtonProc,
			},
		},
	},

};


WND_WIDGET_S stGlbDialogWindow[] = {
	{
		.enType = WND_WIDGET_TYPE_DLG_E,
		//WND_WIDGET_ATTR_U unWidget;
		.stCommon = {
			.stAttr = {
				.s8Name = "ok",
				.u32WndId = 0,
				.u32Group = 0,
				//.u32ParentWndId = HWND_DESKTOP,
				//.u64Style = WS_VISIBLE | WS_BORDER,
				//.u64ExStyle = WS_EX_NONE,
				.u32Id = WND_DLG_TYPE_OK,
				.u32WndX = UI_COORD_MAX/4,
				.u32WndY = UI_COORD_MAX/4,
				.u32WndWidth = UI_COORD_MAX/2,
				.u32WndHeight = UI_COORD_MAX/2,
				.enBkColor = UI_COLOR_DEFAULT_E,
				//.enBkColor = UI_COLOR_DARKRED_E,
				.bShowNow = true,
				.bBorder = true,
				.bCaption = true,
			},
			.stFops = {
				.CallBack = WND_DLG_Proc,
			},
		},

		.pstWidget = stGlbWndDlgOk,
		.s32WidgetNum = sizeof(stGlbWndDlgOk) / sizeof(stGlbWndDlgOk[0]),
	},

};

unsigned int WND_DLG_CreatById(unsigned int u32DlgType, unsigned int u32Parent)
{
	int s32Num;
	WND_WIDGET_S *pstWnd = stGlbDialogWindow;
	for(s32Num = 0; s32Num < sizeof(stGlbDialogWindow) / sizeof(stGlbDialogWindow[0]); s32Num++) {
		if(u32DlgType == pstWnd[s32Num].stCommon.stAttr.u32Id) {
			return WND_WIDGET_Create(pstWnd, u32Parent);
		}
	}
	return -1;
}


void WND_DLG_DestroyById(unsigned int u32DlgType)
{
	int s32Num;
	WND_WIDGET_S *pstWnd = stGlbDialogWindow;
	for(s32Num = 0; s32Num < sizeof(stGlbDialogWindow) / sizeof(stGlbDialogWindow[0]); s32Num++) {
		if(u32DlgType == pstWnd[s32Num].stCommon.stAttr.u32Id) {
			WND_WIDGET_Destroy(pstWnd);
		}
	}

}


